package _8_adapter

// ---A系统查询用户接口

type ASystemUser struct {
	name string
	addr string
	age  int
}

type ASystemUserService interface {
	getUser(name string) *ASystemUser
}

type ASystemUserServiceImpl struct {
}

var aSystemUserStorage = make(map[string]*ASystemUser)

func (A *ASystemUserServiceImpl) getUser(name string) *ASystemUser {
	return aSystemUserStorage[name]
}

// ---B系统查询用户接口

type BSystemUser struct {
	username string
	userAddr string
	userAge  int
}

type BSystemUserService interface {
	getUser(name string) *BSystemUser
}

type BSystemUserServiceImpl struct {
}

var bSystemUserStorage = make(map[string]*BSystemUser)

func (b *BSystemUserServiceImpl) getUser(name string) *BSystemUser {
	return bSystemUserStorage[name]
}

// --- 新需求：查询用户信息的时候先从A系统查询 查询不到再去B系统去查

type ASystemUserServiceAdapter struct {
	ASystemUserService ASystemUserService
	BSystemUserService BSystemUserService
}

func NewASystemUserServiceAdapter() *ASystemUserServiceAdapter {
	return &ASystemUserServiceAdapter{
		ASystemUserService: &ASystemUserServiceImpl{},
		BSystemUserService: &BSystemUserServiceImpl{},
	}
}

func (A *ASystemUserServiceAdapter) getUser(name string) *ASystemUser {
	user := A.ASystemUserService.getUser(name)
	if user != nil {
		return user
	}
	bUser := A.BSystemUserService.getUser(name)
	if bUser == nil {
		return nil
	}
	user = &ASystemUser{
		name: bUser.username,
		addr: bUser.userAddr,
		age:  bUser.userAge,
	}
	return user
}
